<?php
/**
 * 底层数据库类
 * @author ChunYang.Jiang<jcy8805@foxmail.com>
 * @date 2011-6-22 上午12:22:58
 */
class ModelBase
{
	/**
	 * 执行插入操作
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:55:10
	 * @param unknown_type $server
	 * @param unknown_type $array
	 * @param unknown_type $table
	 */
	public static function queryInsert($table, $array, $reId=true, $server='master')
	{
		$sql = self::_buildInsertSql($array, $table);
		$db = Mysql::getInstance($server);
		$ret = $db->query($sql);
		if ($ret && $reId){
			return $db->getLastInsertId();
		}
		return $ret;
	}
	
	/**
	 * 执行查询操作
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:55:27
	 * @param unknown_type $server
	 * @param unknown_type $table
	 * @param unknown_type $fields
	 * @param unknown_type $where
	 * @param unknown_type $start
	 * @param unknown_type $limit
	 */
	public static function querySelect($table, $fields, $where='', $order='', $start=0, $limit=0, $server='slave')
	{
		if (is_array($fields)){
			$fields = implode('', $fields);
		}
		$sql = "SELECT {$fields} FROM `{$table}` " . self::_buildWhere($where) . self::_buildOrder($order);
		if ($start>=0 && $limit>0){
			$sql .= " LIMIT {$start},{$limit}";
		}
		$result = Mysql::getInstance($server)->query($sql);
		$ret = array();
		if ($result){
			while ($row = mysql_fetch_assoc($result)) {
				$ret[] = $row;
			}
		}
		return $ret;
	}
	
	/**
	 * 执行查询操作，返回单条记录
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:55:41
	 * @param unknown_type $server
	 * @param unknown_type $table
	 * @param unknown_type $fields
	 * @param unknown_type $where
	 */
	public static function querySelectSingle($table, $fields, $where, $server='slave')
	{
		$result = self::querySelect($table, $fields, $where, $order='', 0, 1, $server);
		if ($result){
			return $result[0];
		}else{
			return $result;
		}
	}
	
	/**
	 * 执行更新操作
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:55:58
	 * @param unknown_type $server
	 * @param unknown_type $table
	 * @param unknown_type $array
	 * @param unknown_type $where
	 */
	public static function queryUpdate($table, $array, $where, $server='master')
	{
		$sql = self::_buildUpdateSql($array, $table, $where);
		return Mysql::getInstance($server)->query($sql);
	}
	
	/**
	 * 执行删除操作
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:56:09
	 * @param unknown_type $server
	 * @param unknown_type $table
	 * @param unknown_type $where
	 */
	public static function queryDelete($table, $where, $server='master')
	{
		$sql = "DELETE FROM `{$table}`" . self::_buildWhere($where);
		return Mysql::getInstance($server)->query($sql);
	}
	
	public static function getInsertId()
	{
		return Mysql::getInstance('master')->getLastInsertId();
	}
	/**
	 * 构建插入语句
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:56:26
	 * @param unknown_type $array
	 * @param unknown_type $table
	 */
	private static function _buildInsertSql($array, $table)
	{
		$sql = '';
		if ($array){
			$sql = "INSERT INTO `{$table}` ";
			$fields = array();
			$values = array();
			foreach ($array as $key=>$val){
				$fields[] = "`{$key}`";
				$val = self::_escape($val);
				$values[] = "'{$val}'";
			}
			$sql .=  "(" . implode(',', $fields) . ")VALUES(" . implode(',', $values) . ");"; 
		}
		return $sql;
	}
	
	/**
	 * 构建更新语句
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:56:51
	 * @param unknown_type $array
	 * @param unknown_type $table
	 * @param unknown_type $where
	 */
	private static function _buildUpdateSql($data, $table, $where)
	{
		$sql = "UPDATE `{$table}` SET ";
		if (!$data){
			return false;
		}
		if (is_array($data)) {
			$update = array();	
			foreach ($data as $key=>$val){
				$update[] = "`$key`='" . self::_escape($val) . "'";
			}
			$update = implode(',', $update);
		}else{
			$update = $data;
		}
		return $sql . $update . self::_buildWhere($where);
	}
	
	/**
	 * 构建查询条件
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:57:07
	 * @param unknown_type $where
	 */
	private static function _buildWhere($where)
	{
		$reWhere = '';
		if ($where){
			$reWhere = " WHERE ";
			if (is_array($where)){
				$whereArr = array();
				foreach ($where as $key=>$val){
					$whereArr[] = "`{$key}`='{$val}'";
				}
				$reWhere .= implode(' and ', $whereArr);
			}else{
				$reWhere .= $where;
			}
		}
		return $reWhere;
	}
	
	/**
	 * 构建排序条件
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:57:21
	 * @param unknown_type $order
	 */
	private static function _buildOrder($order)
	{
		$reOrder = '';
		if ($order) {
			$reOrder = " ORDER BY ";
			if (is_array($order)){
				$orderArr = array();
				foreach ($order as $key=>$val){
					$orderArr[] = "`{$order}` {$val}";
				}
				$reOrder .= implode(',', $orderArr);
			}else{
				$reOrder .= $order;
			}
		}
		return $reOrder;
	}
	
	/**
	 * 安全过滤
	 * @author ChunYang.Jiang<jcy8805@foxmail.com>
	 * @date 2011-6-22 上午01:57:51
	 * @param unknown_type $value
	 */
	private static function _escape($value)
	{
		return mysql_escape_string($value);
	}
}